import 'reflect-metadata'
import { RequestHandler } from 'express'
import { LoginController, CrawlerController } from '../controller'

export function use(middleware: RequestHandler) {
  return function (target: LoginController | CrawlerController, key: string) {
    let middlewares: RequestHandler[] =
      Reflect.getMetadata('middlewares', target, key) || []
    if (middlewares.length) {
      middlewares.push(middleware)
    } else {
      middlewares = [middleware]
    }
    Reflect.defineMetadata('middlewares', middlewares, target, key)
  }
}
